//
//  NetworkingHandler.m
//  UI29_Block
//
//  Created by dllo on 16/1/21.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "NetworkingHandler.h"

@implementation NetworkingHandler

+ (void)networkingWithUrl:(NSString *)string completeHandle:(void (^)(NSURLResponse *, NSData *,id))block {

    // 字符串转码
    NSString *codeString = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

    NSURL *url= [NSURL URLWithString:codeString];
    
    // session
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
       
        
        // 会熬主线程(GCD)
        dispatch_async(dispatch_get_main_queue(), ^{
           
            if (data) {
    
            // 数据解析
            id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            // 调用 block
                
                block(response, data, result);
            
            }
            
        });
        
    }];
    
    
    
    [task resume];


}





@end
